package com.zycfc.zsf.boot.mq.rabbitmq.retry;

import com.zycfc.zsf.boot.serializer.*;
import javax.annotation.*;
import com.zycfc.zsf.boot.mq.rabbitmq.util.*;
import java.io.*;
import java.util.*;
import org.slf4j.*;

public class PersistentThread implements Runnable
{
    private static final Logger log;
    private static final String CACHE_FILE_NAME = "retry.cache";
    private final RetryCache retryCache;
    private final ISerializer serializer;
    private final RetryCacheProperties properties;
    private volatile boolean running;
    
    public PersistentThread(final RetryCache retryCache, final ISerializer serializer, final RetryCacheProperties properties) {
        this.running = false;
        this.retryCache = retryCache;
        this.serializer = serializer;
        this.properties = properties;
    }
    
    @PostConstruct
    public void start() {
        if (this.retryCache == null || this.serializer == null) {
            return;
        }
        this.loadMessage();
        final Thread hook = new Thread(new Runnable() {
            @Override
            public void run() {
                PersistentThread.this.persistent();
            }
        });
        Runtime.getRuntime().addShutdownHook(hook);
        final Thread thread = new Thread(this);
        this.running = true;
        thread.start();
    }
    
    @PreDestroy
    public void stop() {
        this.running = false;
    }
    
    @Override
    public void run() {
        while (this.running) {
            this.persistent();
            try {
                Thread.sleep(this.properties.getPersistentInterval());
            }
            catch (InterruptedException e) {
                PersistentThread.log.error(e.getMessage(), (Throwable)e);
                Thread.currentThread().interrupt();
            }
        }
    }
    
    private synchronized void persistent() {
        final Map<String, MessageCache> cacheMap = this.retryCache.getMessageLocalCache();
        final String filePath = this.properties.getLocalCachePath() + "retry.cache";
        if (null != cacheMap && !cacheMap.isEmpty()) {
            try {
                FileUtil.writeFile(filePath, this.serializer.serialize((Object)cacheMap), false);
            }
            catch (Exception e) {
                PersistentThread.log.error(e.getMessage(), (Throwable)e);
            }
        }
        else {
            final File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
        }
    }
    
    private void loadMessage() {
        final byte[] data = FileUtil.readFile(this.properties.getLocalCachePath() + "retry.cache");
        try {
            final Map<String, MessageCache> map = (Map<String, MessageCache>)this.serializer.deserialize(data);
            this.retryCache.putAll(map);
        }
        catch (Exception e) {
            PersistentThread.log.error(e.getMessage(), (Throwable)e);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)PersistentThread.class);
    }
}
